package com.vma.interfaceShoes.controller;

import com.jfinal.core.Controller;
import com.jfinal.kit.HttpKit;
import com.vma.interfaceShoes.util.ReturnsUtil;
import com.vma.user.service.AddressService;
import net.sf.json.JSONObject;

/**
 * Created by chenzui on 2017/4/12.
 */
public class AddressController extends Controller {
    /**
     * 新增地址
     */
    public void add(){
        try{

            Integer userId = getParaToInt("userId");
            String location = getPara("location");
            String desc = getPara("desc");
            String phone = getPara("phone");
            String name = getPara("name");
            String province = getPara("province");
            String city = getPara("city");
            String area = getPara("area");
            String defaultStr = getPara("defaultStr");
            AddressService.add(userId,phone,name,location,desc,province,city,area,defaultStr);
            renderJson(ReturnsUtil.returnSuccess("成功"));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }

    }

    /**
     * 查询地址列表
     */
    public void qryAddressList(){
        try {
            renderJson(ReturnsUtil.returnSuccess(AddressService.qryAddressList(getParaToInt("userId"))));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

    /**
     * update address info
     */
    public void updateAddress(){
        try {

            Integer userId = getParaToInt("userId");
            String location = getPara("location");
            String desc = getPara("desc");
            String phone = getPara("phone");
            String name = getPara("name");
            Integer id = getParaToInt("id");
            String province = getPara("province");
            String city = getPara("city");
            String area = getPara("area");
            String defaultStr = getPara("defaultStr");
            AddressService.update(id,location,desc,phone,name,province,city,area,defaultStr);
            renderJson(ReturnsUtil.returnSuccess("更新地址成功"));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

    /**
     * delete address :set status = 0
     */
    public void delete(){
        try{
            Integer id = getParaToInt("id");
            AddressService.delete(id);
            renderJson(ReturnsUtil.returnSuccess("删除地址成功"));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

    public void getAddressById(){
        try{
            Integer id = getParaToInt("addressId");
            renderJson(ReturnsUtil.returnSuccess(AddressService.getAddressById(id)));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }
}
